<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\SmsTpl;
use App\Constants\SmsConstant;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Tab;

class SmsTplController extends AdminController
{
    public function index(Content $content)
    {
        $tab = Tab::make();
        $tab->addLink('短信配置', admin_route('sms_setting.index'));
        $tab->add('模板管理', $this->grid(), true);
        $tab->addLink('发送记录', admin_route('sms_log.index'));
        return $content->body($tab->withCard());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new SmsTpl(), function (Grid $grid) {
            $grid->model()->orderByDesc('id');

            $grid->column('id');
            $grid->column('title');
            $grid->column('type');
            $grid->column('template');
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->equal('id')->width(2);
                $filter->like('title')->width(2);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new SmsTpl(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('type');
            $show->field('content');
            $show->field('template');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new SmsTpl(), function (Form $form) {
            $form->display('id');
            $form->text('title')->required();
            $form->select('type')->options(SmsConstant::TPL_TYPE_MAP)->required();
            $form->textarea('template')->help('一行一条，格式：驱动名|模板   aliyun|xxx')->required();
            $form->textarea('content')->help('一行一条，格式：驱动名|模板   aliyun|xxx')->required();

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
